package financier.jfz.com.babycare.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import financier.jfz.com.babycare.R;
import financier.jfz.com.babycare.base.BaseViewGroup;
import financier.jfz.com.babycare.base.BaseViewGroup.GroupAdapter;
import financier.jfz.com.babycare.base.Callback;
import financier.jfz.com.babycare.bean.AddItemBean;
import financier.jfz.com.babycare.bean.TableRowData;

public class EditeDialog {
    View rootView;
    List<AddItemBean> data = new ArrayList<>();
    BaseViewGroup listView;
    Adapter adapter;
    Callback mcallback;
    public void show(Activity context, final List<AddItemBean> data,Callback callback) {
        this.mcallback=callback;
        this.data.clear();
        this.data.addAll(data);
        rootView = LayoutInflater.from(context).inflate(R.layout.tab_edit_dialog_layout, null);
        listView = rootView.findViewById(R.id.list_view);
        adapter=new Adapter();
        listView.setAdapter(adapter);
      new AlertDialog.Builder(context)
                    .setTitle("新增/编辑列表")
                    .setView(rootView)
                    .setCancelable(false)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            TableRowData rowdata= new TableRowData();
                            rowdata.id=UUID.randomUUID().toString();
                            for(Holder holder: adapter.getAllHolder()){
                                TableRowData.TableItemData itemData=new TableRowData.TableItemData();
                                itemData.key=data.get(holder.getAdapterPosition()).id;
                                itemData.labData=String.valueOf(holder.msg.getText());
                                rowdata.setData(itemData,holder.getAdapterPosition());
                            }
                           mcallback.ececute(rowdata);
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {

          }
      })
              .create().show();
        }

    class Adapter extends GroupAdapter<EditeDialog.Holder> {

        @Override
        public int count() {
            return data.size();
        }

        @Override
        public Holder creatHolder(ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ab_edit_dialog_item, parent, false);
            return new Holder(view);
        }

        @Override
        public void bind(int position, Holder holder) {
            holder.lab.setText(data.get(position).name);
            holder.msg.setHint("请输入" + data.get(position).name);
            if ("num".equals(data.get(position).type)) {
                holder.msg.setInputType(InputType.TYPE_CLASS_NUMBER);
            } else {
                holder.msg.setInputType(InputType.TYPE_CLASS_TEXT);
            }
        }
    }

    class Holder extends BaseViewGroup.GroupHolder {
        EditText msg;
        TextView lab;
        Holder(View view) {
            super(view);
            msg = view.findViewById(R.id.text_msg);
            lab = view.findViewById(R.id.lab);
        }
    }
}
